﻿using System;
using System.Collections.Generic;
using System.Linq;
using Adp;
using Adp.Common;
using Yqd.Enm;
using Yqd.Models;

namespace Yqd.Core
{
    public class SuperviseService
    {
        /// <summary>
        /// 获取办理事项
        /// </summary>
        public static (string TableName, string KeyName) GetTableName(事项类型 ItemType)
        {
            string tblname = "", keyname = "";
            switch (ItemType)
            {
                case 事项类型.钉办督导:
                    tblname = "Yqd_Supervise";
                    keyname = "SuperviseID";
                    break;
                case 事项类型.协同管理:
                    tblname = "Yqd_Coordinate";
                    keyname = "CoordinateID";
                    break;
                case 事项类型.精准督查:
                    tblname = "Yqd_ExactSupervise";
                    keyname = "SuperviseID";
                    break;
                case 事项类型.专项交办:
                    tblname = "Yqd_SpecialSupervise";
                    keyname = "SuperviseID";
                    break;
            }
            return (tblname, keyname);
        }

        /// <summary>
        /// 更新完成状态
        /// </summary>
        public static int UpdateComplete(int ItemID, 事项类型 ItemType)
        {
            var ctx = Fn.GetService<YqdContext>();
            var ret = 0;
            //var complete = ctx.Yqd_JobPlan.Any(it => it.ItemID == ItemID && it.ItemType == ItemType && it.PlanStatus == 工作计划状态.未完成);
            //if (!complete)
            //{
            var (tblname, keyname) = GetTableName(ItemType);

            TaskLogService.AddLog(0, $"进展反馈{ItemID},{tblname},{keyname},{ItemID},已完成");
            ret = ctx.Database.ExecuteSql("Update " + tblname + " set Status={1} Where " + keyname + "={0}", ItemID, (int)任务状态.已完成);

            Logs.Warning<SuperviseHostService>("Update " + tblname + " set Status=" + (int)任务状态.已完成 + " Where " + keyname + "=" + ItemID);
            //}
            return ret;
        }
        public static int UpdateComplete(int ItemID, 事项类型 ItemType, 任务状态 status)
        {
            var ctx = Fn.GetService<YqdContext>();
            var ret = 0;
            var (tblname, keyname) = GetTableName(ItemType);
            TaskLogService.AddLog(0, $"进展反馈{ItemID},{tblname},{keyname},{ItemID},{status.ToString()}");
            //if (complete)
            //{
            //if (isUpdate)
            //{
                ret = ctx.Database.ExecuteSql("Update " + tblname + " set Status={1} Where " + keyname + "={0}", ItemID, (int)status);

                Logs.Information<SuperviseHostService>("更新结果：" + ret + ",Update " + tblname + " set Status=" + (int)status + " Where " + keyname + "=" + ItemID);
            //}
            //}
            return ret;
        }
        /// <summary>
        /// 获取督查单号
        /// </summary>
        public static string GetSuperviseNo(long TaskID)
        {
            var ctx = Fn.GetService<YqdContext>();
            var s = ctx.Yqd_SuperviseNo.FirstOrDefault(it => it.TaskID == TaskID);
            if (s != null) return s.SNo;
            var sno = DateTime.Now.Year.ToString();
            var rs = ctx.Yqd_SuperviseNo.Where(it => it.SNo.Substring(0, 4) == sno).OrderByDescending(it => it.ID);
            if (!rs.Any()) sno += "001";
            else sno += (int.Parse(rs.First().SNo.Substring(4)) + 1).ToString("000");
            ctx.Yqd_SuperviseNo.Add(new Yqd_SuperviseNo
            {
                TaskID = TaskID,
                SNo = sno,
                Instime = DateTime.Now
            });
            ctx.SaveChanges();
            return sno;
        }

        /// <summary>
        /// 获取进展反馈周期
        /// </summary>
        /// <param name="loop">周期</param>
        /// <param name="feedbackTime">反馈时间</param>
        /// <param name="CompleteLimit"></param>
        /// <param name="Days"></param>
        /// <returns></returns>
        public static string GetFbLoopDate(进展反馈周期 loop, DateTime feedbackTime, DateTime CompleteLimit, int Days = 0)
        {
            switch (loop)
            {
                case 进展反馈周期.报送进展情况:
                    return "";
                case 进展反馈周期.一次性:
                    return CompleteLimit.ToString("yyyy-MM-dd");
                default:
                    if (loop == 进展反馈周期.每月25日)
                    {
                        DateTime startMonth = CompleteLimit.AddDays(1 - CompleteLimit.Day);
                        return startMonth.AddDays(24).ToString("yyyy-MM-dd");
                    }
                    else if (loop == 进展反馈周期.每月28日)
                    {
                        DateTime startMonth = CompleteLimit.AddDays(1 - CompleteLimit.Day);
                        return startMonth.AddDays(27).ToString("yyyy-MM-dd");
                    }
                    //else if (loop == 进展反馈周期.每旬最后一天)
                    //{
                    //    DateTime dt = DateTime.Now;
                    //    DateTime startMonth = dt.AddDays(1 - dt.Day);
                    //    var date = Convert.ToInt32(dt.ToString("dd"));
                    //    if (date <= 10)
                    //    {
                    //        return startMonth.AddDays(10).ToString("yyyy-MM-dd");
                    //    }
                    //    else if (date > 10 && date <= 20)
                    //    {
                    //        return startMonth.AddDays(20).ToString("yyyy-MM-dd");
                    //    }
                    //    else
                    //    {
                    //        return startMonth.AddMonths(1).AddDays(-1).ToString("yyyy-MM-dd"); //本月月末
                    //    }
                    //}
                    else
                    {
                        var ts = loop == 进展反馈周期.自定义 ? Days : (int)loop;
                        var dt = feedbackTime.Date.AddDays(ts).Date;
                        //return CompleteLimit.Subtract(dt).Days < ts ? CompleteLimit.ToString("yyyy-MM-dd") : dt.ToString("yyyy-MM-dd");
                        return dt.ToString("yyyy-MM-dd");
                    }
            }
        }
    }
}